*==============================================================================*
* Regression Analysis
*==============================================================================*

ssc install coefplot, replace
ssc install scheme-burd, replace
ssc install winsor2, replace
set scheme burd

/* if we want to change the mean value on the plot, the code should be run in sections */
*==============================================================================*
* Weighted interest rates
cd "P:\ECB business areas\DGR\Staff\papouts\Research\Active\Firm Heterogeneities"
global project "P:\ECB business areas\DGR\Staff\papouts\Research\Active\Firm Heterogeneities"
global data "${project}\Final datasets\"
global output "${project}\Final charts\Revision"

use "${data}\instr_lvl_dt_f.dta", clear

* drop missing wir
drop if wir_byloantype == .

sum wir_byloantype, d

*Drop missing SIZE classification
tab sz_f
drop if sz_f == . 

*Drop missing NACE code
drop if nace_code == ""
tab nace_code
egen sctr_f = group(nace_code)

*Drop missing CNTRY code
drop if cntry_dbtr == ""
tab cntry_dbtr
drop if inlist(cntry_dbtr, "CY", "EE", "LT", "LU", "LV", "MT", "SI", "SK")
encode cntry_dbtr, gen(cntry_f)

* drop instruments different from loans and credit lines
drop if inlist(typ_instr_corr, "Revolving credit", "Finance leases", "Trade receivables")
tab typ_instr_corr

*Loan type
encode typ_instr_corr, gen(instr_f)

* calculate loan type share within debtor
egen dbtr_ona = total(ona_intrate_c_ttl), by(dbtr_id)
gen ONA_DBTR_LOAN_SHR = ona_intrate_c_ttl/dbtr_ona

* drop if missing loan share
sum ONA_DBTR_LOAN_SHR
drop if ONA_DBTR_LOAN_SHR == .

* mean value
sum wir_byloantype

/*
tab CNTRY

preserve 
collapse W_RT_BYTYPE [aweight=ONA_DBTR_LOAN_SHR],by(CNTRY)
list 
restore
*/

*==============================================================================*
* interest rates
*==============================================================================*

* unconditional regression
reg wir_byloantype  ibn.cntry_f [aweight=ONA_DBTR_LOAN_SHR], noconstant baselevels
estimates store r_uncond

* testing the equality of multiple coefficients
testparm i(1/11).cntry_f, equal

matrix coeff2 = e(b)'
matrix list coeff2
svmat coeff2
gen n = _n
preserve
	keep n coeff21
	keep if n >= 1 & n <12
	save "${data}\uncond_coeff_lterm.dta", replace
	egen mu_uncond = mean(coeff21)
	drop n coeff21
	duplicates drop *, force
	gen n = _n
	save "${data}\uncond_lterm.dta", replace
restore

* merge the unconditional coefficient for new instruments (again only loans and credit lines)
merge 1:1 n using "${data}\uncond_coeff_newlterm.dta", gen(_merge)


gen c = ""
replace c = "AT" if _n == 1
replace c = "BE" if _n == 2
replace c = "DE" if _n == 3
replace c = "ES" if _n == 4
replace c = "FI" if _n == 5
replace c = "FR" if _n == 6
replace c = "GR" if _n == 7
replace c = "IE" if _n == 8
replace c = "IT" if _n == 9
replace c = "NL" if _n == 10
replace c = "PT" if _n == 11

* sort as large firms - from python charts
gen order = 0
replace order = 1 if c == "GR"
replace order = 2 if c == "IE"
replace order = 3 if c == "NL"
replace order = 4 if c == "PT"
replace order = 5 if c == "IT"
replace order = 6 if c == "DE"
replace order = 7 if c == "FR"
replace order = 8 if c == "ES"
replace order = 9 if c == "BE"
replace order = 10 if c == "AT"
replace order = 11 if c == "FI"

* list coefficients
* coeff31 - unconditional new long-term instruments
* coeff21 - unconditional long-term instruments
list c coeff31 coeff21 


* chart
graph bar coeff31 coeff21 , over(c, sort(order)) ///
ylabel(0(0.01)0.06,nogextend  ) plotregion(lcolor(black)) ///
ytitle("regression coefficient") bar(1, color(olive_teal) lcolor(black)) ///
bar(2, color(gold) lcolor(black)) legend(label(1 "New long-term instruments Unconditional") label(2 "Long-term instruments Unconditional") position(12)) ///
*yline(0.031, lcolor(black) lstyle("-")) 
graph export "${output}\Section6_reg_intrate_unw_newlterm_ordbysz.pdf", replace /* width(2500)	*/	


translate @Results "${output}\6_reg_intrate_unw_newlterm_comparison_logfile.txt", replace



